<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use think\Controller;
use think\Request;

class Express extends Backend
{
    public function __construct()
    {
        parent::__construct();

        $this->ExpressModel = model('Express.Express');
    }

    //快递公司查询
    public function expressindex()
    {
        $expressList = $this->ExpressModel->paginate(9);;
        $navTitle = '配送平台';
        return $this->fetch('', [
            'navTitle' => $navTitle,
            'expressList' => $expressList,
        ]);
    }

    //快递公司新增
    public function expressadd()
    {
        if ($this->request->isPost() && $this->request->isAjax()) {
            $action = $this->request->param('action');
            if ($action === 'name') {
                $name = $this->request->param('name');
                $nameResult = $this->ExpressModel->where('name', $name)->find();
                if ($nameResult) {
                    $this->error('配送公司名称已存在');
                } else {
                    $this->success('配送公司名称可以使用');
                }
            }
        } else if ($this->request->isPost()) {
            $params = $this->request->param();

            //封装数据
            $ExpressData = [
                'name' => $params['name'],
                'prename' => $params['prename']
            ];

            $result = $this->ExpressModel->validate('common/Express/Express')->save($ExpressData);
            // $result = FALSE;
            if ($result === FALSE) {
                $this->error($this->ExpressModel->getError());
            } else {
                $this->success('新增快递公司成功', url('admin/express/expressindex'));
            }
        }
        $navTitle = '配送平台';
        return $this->fetch('', [
            'navTitle' => $navTitle,
        ]);
    }

    //快递公司编辑
    public function expressedit()
    {
        $expId = $this->request->param('id', 0);
        if ($this->request->isPost()) {
            $params = $this->request->param();

            //封装数据
            $ExpressData = [
                'id' => $expId,
                'name' => $params['name'],
                'prename' => $params['prename']
            ];

            $result = $this->ExpressModel->validate('common/Express/Express')->isUpdate(true)->save($ExpressData);
            // $result = FALSE;
            if ($result === FALSE) {
                $this->error($this->ExpressModel->getError());
            } else {
                $this->success('更新快递公司成功', url('admin/express/expressindex'));
            }
        }
        $expressData = $this->ExpressModel->find($expId);
        $navTitle = '配送平台';
        return $this->fetch('', [
            'navTitle' => $navTitle,
            'expressData' => $expressData
        ]);
    }

    //快递公司批量删除和单选删除
    public function expressdel()
    {
        $action = $this->request->param('action', '');
        $result = null;
        //商品分类单选永久删除
        if ($action == 'expressDel') {
            $id = $this->request->param('id', 0);
            $result = $this->ExpressModel->destroy($id, true);
        }
        //商品分类多选永久删除
        if ($action == 'expressDelAll') {
            $list = $this->request->param();
            $result = $this->ExpressModel->destroy($list['list']);
        }
        if ($result === FALSE) {
            $this->error($this->ExpressModel->getError());
        } else {
            $this->success('删除成功', url('admin/express/expressindex'));
        }
    }
}
